import Vue from 'vue'
import Router from 'vue-router'
import Index from '../pages/index' //首頁
import Login from '../pages/login' //登錄
import Dashboard from '../pages/dashboard/dashboard' //儀錶盤
import Role from '../pages/role/role' //角色管理
import RoleList from '../pages/role/roleList' //角色管理 - 角色列表
import RoleAdd from '../pages/role/roleAdd' //角色管理 - 新增角色
import RoleEdit from '../pages/role/roleEdit' //角色管理 - 编辑角色
import Order from '../pages/order/order' //订单管理
import OrderAdd from '../pages/order/orderAdd' //订单管理 - 新增订单
import OrderEdit from '../pages/order/orderEdit' //订单管理 - 编辑订单
import OrderList from '../pages/order/orderList' //订单管理 - 订单列表
import Product from '../pages/product/product' //订单管理
import ProductAdd from '../pages/product/productAdd' //产品管理 - 新增产品
import ProductEdit from '../pages/product/productEdit' //产品管理 - 编辑产品
import ProductList from '../pages/product/productList' //产品管理 - 产品列表
import Member from '../pages/member/member' //会员管理
import MemberAdd from '../pages/member/memberAdd' //会员管理 - 新增会员
import MemberEdit from '../pages/member/memberEdit' //会员管理 - 编辑会员
import MemberList from '../pages/member/memberList' //会员管理 - 会员列表

Vue.use(Router)
const route = new Router({
  routes: [
    {
      path: '/',
      component: Index,
      children: [
        {
          path: '/',
          name: 'Dashboard',
          component: Dashboard,
          meta: {
            title: '数据仪表盘',
            index: 0
          }
        },
        {
          path: '/Role',
          component: Role,
          children: [
            {
              path: '/',
              name: 'Role',
              component: RoleList,
              meta: {
                title: '角色列表',
                index: 1
              }
            },
            {
              path: 'RoleAdd',
              name: 'RoleAdd',
              component: RoleAdd,
              meta: {
                title: '新增角色',
                index: 1
              }
            },
            {
              path: 'RoleEdit/:id',
              name: 'RoleEdit',
              component: RoleEdit,
              meta: {
                title: '编辑角色',
                index: 1
              }
            }
          ]
        },
        {
          path: '/Product',
          component: Product,
          children: [
            {
              path: '/',
              name: 'Product',
              component: ProductList,
              meta: {
                title: '产品列表',
                index: 2
              }
            },
            {
              path: 'ProductAdd',
              name: 'ProductAdd',
              component: ProductAdd,
              meta: {
                title: '新增产品',
                index: 2
              }
            },
            {
              path: 'ProductEdit/:id',
              name: 'ProductEdit',
              component: ProductEdit,
              meta: {
                title: '编辑产品',
                index: 2
              }
            }
          ]
        },
        {
          path: '/Order',
          component: Order,
          children: [
            {
              path: '/',
              name: 'Order',
              component: OrderList,
              meta: {
                title: '订单列表',
                index: 3
              }
            },
            {
              path: 'OrderAdd',
              name: 'OrderAdd',
              component: OrderAdd,
              meta: {
                title: '新增订单',
                index: 3
              }
            },
            {
              path: 'OrderEdit/:id',
              name: 'OrderEdit',
              component: OrderEdit,
              meta: {
                title: '编辑订单',
                index: 3
              }
            }
          ]
        },
      ]
    },
    {
      path: '/Login',
      name: 'Login',
      component: Login,
    },
  ]
});


route.beforeEach((to, from, next) => {
  let token = window.localStorage.getItem('token');
  if (to.matched.some(record => record.meta.requiresAuth) && (!token || token === null)) {
    next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
  } else {

    Vue.prototype.axios.interceptors.response.use(
      response => {

        if (response.data.code === '1016') {
          next({
            path: '/login',
            query: { redirect: to.fullPath }
          });

          return false;
        }

        return response;
      },
      error => {
        if (error.response) {
          switch (error.response.status) {
            case 401:
              next({
                path: '/login',
                query: { redirect: to.fullPath }
              });
              break;
          }
        }
      });

    next()
  }
});

export default route
